package com.hooker.common;

import android.net.Uri;

import de.robv.android.xposed.XC_MethodHook;

public class Reflect {

    public static Class<?>[] getParameterClasses(ClassLoader classLoader, Object[] parameterTypesAndCallback) {
        Class[] parameterClasses = null;

        for (int i = parameterTypesAndCallback.length - 1; i >= 0; i--) {
            Object type = parameterTypesAndCallback[i];
            if (type == null) {
                throw new RuntimeException("parameter type must not be null", (Throwable) null);
            }

            if (parameterClasses == null) {
                parameterClasses = new Class[i + 1];
            }

            if (type instanceof Class) {
                parameterClasses[i] = (Class) type;
            } else {
                if (!(type instanceof String)) {
                    throw new RuntimeException("parameter type must either be specified as Class or String", (Throwable) null);
                }

                try {
                    parameterClasses[i] = classLoader.loadClass((String) type);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

        if (parameterClasses == null) {
            parameterClasses = new Class[0];
        }

        return parameterClasses;
    }

    public static Class getAvailableClass(String name, ClassLoader loader, String method, Class<?>... paramType) {
        Class cls = null;
        try {
            cls = loader.loadClass(name);
            while (cls != null) {
                try {
                    if (cls != null && cls.getDeclaredMethod(method, paramType) != null) {
                        break;
                    }
                    cls = cls.getSuperclass();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                    cls = cls.getSuperclass();
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return cls;
    }
}
